<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core'%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" /><!-- 控制浏览器文件模式为IE8 -->
<title>功能項管理</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="js/artDialog/skins/default.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="css/elegant/om-all.css" />

<script src="js/artDialog/artDialog.js?skin=blue"></script>
<script src="js/artDialog/plugins/iframeTools.js"></script>
<script src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/operamasks-ui.min.js"></script>
<script type="text/javascript" src="js/selfpagination.js"></script>
<script src="js/common.js"></script>
<!-- <script language="javascript">
	var j = jQuery.noConflict();
	j.ajaxSetup({
		cache:false,
		async: false
	});
</script> -->
<script type="text/javascript">
	/* (function (config) {
	    config['lock'] = true;
	    config['fixed'] = true;
	    config['okVal'] = 'Ok';
	    config['cancelVal'] = 'Cancel';	  
	})(art.dialog.defaults);
	 j.omMessageBox.waiting({
         title:'請稍後',
         content:'服務器正在處理您的請求，請稍後...',
     }); */

	 function jumpPage(jumpPage, pageSize) {		
			var functionName = $("#functionName").val();			
			var functionCode = $("#functionCode").val();			
			var param = "&curPage=" + jumpPage;
				param += "&functionName=" + functionName;
				param += "&functionCode=" + functionCode;
			
			window.location.href = "${pageContext.request.contextPath}/adminFunction.do?action=list" + param;
		}

	 
	 
	 function search() {
		$("#searchKey").val("search");
		$("#currentPage").val("1");
		var flag=true;
		if(flag)
		{
			$.omMessageBox.waiting({
		         title:'請稍後',
		         content:'服務器正在處理您的請求，請稍後...',
		     });
			document.forms[0].submit();
		}		
	} 
	
	/* function reset()
	{
		j("#functionName").val("");
		j("#functionCode").val("");
	} */
	
	function deleteFunction(functionId){
		if(functionId.length < 10){
			 $.omMessageBox.alert({
				content:'系統設定，不可刪除！',
				onClose:function(v){}
			});				
		}else{					
			$.omMessageBox.confirm({
	           title:'確認刪除',
	           content:'刪除功能後，該信息不可恢復，您確定要這樣做嗎？',
	           onClose:function(v){
	               if(v){
	           			$.ajax( {
	           				url : './adminFunction.do?action=delete&functionId=' + functionId,
	           				date : '',
	           				type : 'post',
	           				dateType : 'xml',
	           				timeout : 100000,
	           				error : function(xml) {
	           					$.omMessageBox.alert({
        					           content:'響應失敗！',
        					           onClose:function(v){}
        					    });
	           				},
	           				success : function(xml) {
	           					var $message = $(xml).find("del").children("stat").text();
	           					if ($message == 'haveChild') {
	           						$.omMessageBox.alert({
	           					           content:'刪除失敗，原因：該功能項須具有子功能項！',
	           					           onClose:function(v){}
	           					    });
	           					}else if ($message == 'ok') {
	           						$.omMessageBox.alert({
	           					           content:'删除成功！',
	           					           onClose:function(v){
	           					        	   document.location.href = './adminFunction.do?action=list';
	           					           }
	           					    });
	           					}else {
	           						art.dialog.tips('删除失敗！', 3);
	           						return false;
	           					}
	           				}
	           			});
	               }
	           }
	    });
	}		
}
	function addOperate(functionId){
		var w = screen.width;
		var h = screen.height;
		if (w > 1100)
			w = 500;
		if (h > 700)
			h = 300;
		var url = "./adminFunction.do?action=toAddOption&functionId="+functionId+"&time="+new Date().getTime();	
		if (browser_type() == 'safari-or-chrome') {
			strFeatures = "height="
					+ h
					+ ",width="
					+ w
					+ ",top=0,left=100,toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no, status=no,z-look=yes";
			window.open(url, "newwindow", strFeatures);
		} else {
			strFeatures = "dialogWidth=" + w + "px;dialogHeight=" + h
					+ "px;;scrollbars=yes;status=yes;";
			window.showModalDialog(url, window, strFeatures);
		}
		qryOption(functionId);
	}
	
	function deleteOption(optionId,functionId){
		$.ajax({
			url : "${pageContext.request.contextPath}/adminFunction.do?action=deleteOption",
			cache : false,
			async : false,
			type : 'post',
			dataType : "text",
			data : {
				optionId : optionId,
				functionId : functionId
			},
			error : function(){//请求失败处理函数
				alert("操作失敗,請聯繫管理員!");
			},
			success : function(json){
				if(json == "SUCCESS"){
					alert("刪除成功!");
					qryOption(functionId);
				}else{
					alert("刪除失敗!");
				}
			}
		});	
		
	}
	
	function qryOption(functionId){
		$.ajax({
			url : "${pageContext.request.contextPath}/adminFunction.do?action=qryOption",
			cache : false,
			async : false,
			type : 'post',
			dataType : "text",
			data : {
				functionId : functionId
			},
			error : function(){//请求失败处理函数
				alert("操作失敗,請聯繫管理員!");
			},
			success : function(json){
				var datas = eval('('+json+')').root;
				var ele=$("#option");
				ele.empty();	
				ele.append("<tr><th>序號</th><th>功能項名稱</th><th>操作編碼</th><th>描述</th><th>操作</th></tr>");
				for ( var i = 0; i < datas.length; i++) {
					ele.append("<tr><td>"+(i+1)+"</td><td>"+datas[i].functionName+"</td><td>"+datas[i].optionId+"</td><td>"+datas[i].optionDsc+"</td><td><a href='#"+datas[i].functionId+"' id='"+datas[i].functionId+"' onclick=\"deleteOption('"+datas[i].optionId+"','"+datas[i].functionId+"');\">刪除</a></td></tr>");
				}
			}
		});	
	}
</script>
</head>
<body>

	<div class="place">
		<span>位置：</span>
		<ul class="placeul">
	    <li>首頁</li>
		<li>系統管理</li>
	    <li><a href="#" onclick="javascript:window.location.href='./adminFunction.do?action=list'"> 功能項管理</a></li>
		<li>功能項列表</li>
	    </ul>
	</div>
	<div class="rightinfo">
		<form id="form1" action="./adminFunction.do?action=list" method="post">
			<div class="tools">

				<ul class="toolbar">
					<li onclick="javascript:window.location.href='./adminFunction.do?action=toAdd'"><span><img src="images/t01.png" /></span>新增功能項</li>
				</ul>
			</div>

			<ul class="seachform">
				<li>
					<label>功能項名稱：</label>
					<input name="functionName" id="functionName" value="${functionName}" type="text" class="scinput" autocomplete="off"/>
				</li>
				<li>
					<label>功能項編碼：</label>
					<input name="functionCode" id="functionCode" value="${functionCode}" type="text" class="scinput" autocomplete="off"/>
				</li>
				<li><label>&nbsp;</label><input type="button" onclick="search()" class="scbtn" value="查詢" /></li>
			</ul>

			<table class="tablelist">
				<thead>
					<tr>
						<th>序號</th>
						<th>功能項名稱</th>
			            <th>功能項編碼</th>
			            <th>上一級功能項</th>
			            <th>功能項路徑</th>
			            <th>描述</th>                 
			            <th>創建日期</th>     
			            <th>操作</th>
					</tr>
				</thead>
				<tbody>
					<c:set var="i" value="${(paginationSupport.curPage-1)*(paginationSupport.countPerPage)}" /> 
				<c:forEach var="list" items="${paginationSupport.resultList}">
					<c:set var="i" value="${i+1}" />
						<tr>
							<td>${i}</td>
							<td><a href="#" style="color:blue" onclick="qryOption('${list.functionId}');">${list.functionName}</a></td>
				            <td>${list.functionCode}</td>
				            <td>${list.parentName}</td>
				            <td>${list.functionUrl}</td>
				            <td>${list.description}</td>
				            <td><fmt:formatDate value="${list.createdTime}" type="both" pattern="yyyy-MM-dd HH:mm"/></td>  
				            <td>
				            	<a href="#" onclick="addOperate('${list.functionId}')"> 新增</a> 
				            	<a href="javascript:window.location.href='./adminFunction.do?action=special&functionId=${list.functionId}';"> 修改</a> 
								<a href="#" onclick="deleteFunction('${list.functionId}')"> 刪除</a>        
				            </td>
							<td>
								
							</td>
						</tr>
					</c:forEach>
				</tbody>
			</table>
			<%-- <div class="pagin" style="float: right; padding-right: 80px;">
				<c:import url="pagination.jsp"></c:import>
			</div> --%>
			<div class="pagin" style="float: right; padding-right: 30px;">
				<input type="button" id="firstPage" class="grayBtn1" onclick="getFirstPage();" value="首頁"/>
				<input type="button" id="perPage" class="grayBtn1"onclick="getPerPage();" value="上一頁"/>
				<input type="button" id="nextPage" class="grayBtn1" onclick="getNextPage()" value="下一頁"/>
				<input type="button" id="lastPage" class="grayBtn1" style="float:none;"onclick="javascript:getLastPage();" value="尾頁"/>
				${paginationSupport.curPage}/${paginationSupport.countPerPage}	共${paginationSupport.totalCount}條
				<input name="goPage" id="jumpPage" size="5" style="width:50px" class="pages" type="text" value="${paginationSupport.curPage}" /> 頁
				<input type="button" id="jump" class="grayBtn1" onclick="freeJump();" value="GO"/>
				<input type="hidden" id="pageIndex" value="${paginationSupport.curPage}" /> 
				<input type="hidden" id="pageCount" value="${paginationSupport.totalPage}" />
			</div>	
		</form>
		<div style="position:relative;top:10px">
			<table class="tablelist"  id="option">
				
			</table>
	 	</div>
	</div>
	<script type="text/javascript">
		$('.tablelist tbody tr:odd').addClass('odd');
	</script>
	
</body>
</html>